package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.outto.dto.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 淘号号 属性匹配
 */
@Slf4j
@Service
public class TAOHAOHAOAttr {

    public  List<GamesRes> getGames(String open_id, String key) {
        String url = "http://api.hhaohao.com/open/all_game";
        Map<String, String> params = new HashMap<>();
        params.put("open_id", open_id);
        params.put("key", key);
        String result = HttpUtil.doGet(url, params, null);
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            gamesRes.setId(jsonObject.getString("game_id"));
            gamesRes.setGameName(jsonObject.getString("game_name"));
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    public  List<InterServerRes> getServer(String open_id, String key, String gameId) {
        String game_id = null;
        String business_id = null;
        {
            String url = "http://api.hhaohao.com/open/all_game";
            Map<String, String> params = new HashMap<>();
            params.put("open_id", open_id);
            params.put("key", key);
            String result = HttpUtil.doGet(url, params, null);
            JSONObject resJson = JSONObject.parseObject(result);
            List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
            for (JSONObject jsonObject : jsonObjects) {
                if (Objects.equals(jsonObject.getString("game_id"), gameId)) {
                    game_id = jsonObject.getString("game_id");
                    business_id = jsonObject.getString("business_id");
                    break;
                }
            }
        }
        if (StringUtils.isEmpty(game_id)|| StringUtils.isEmpty(business_id)){
            throw new GlobalException("为获取到游戏属性");
        }
        String url = "http://api.hhaohao.com/open/game/properties";
        Map<String, String> params = new HashMap<>();
        params.put("open_id", open_id);
        params.put("key", key);
        params.put("game_id",game_id );
        params.put("business_id",business_id );
        String result = HttpUtil.doGet(url, params, null);
        JSONObject resJson = JSONObject.parseObject(result);
        JSONObject data = resJson.getJSONObject("data");
        JSONArray partitions = data.getJSONArray("partitions");
        List<InterServerRes> interServerRes = new ArrayList<>();
        for (Object partition : partitions) {
            InterServerRes res = new InterServerRes();
            res.setId(partition.toString());
            res.setName(partition.toString());
            interServerRes.add(res);
        }
        return interServerRes;
    }

    public  List<InterAttrRes> getPropety(String open_id, String key, String gameId, String type) {
        String game_id = null;
        String business_id = null;
        {
            String url = "http://api.hhaohao.com/open/all_game";
            Map<String, String> params = new HashMap<>();
            params.put("open_id", open_id);
            params.put("key", key);
            String result = HttpUtil.doGet(url, params, null);
            JSONObject resJson = JSONObject.parseObject(result);
            List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
            for (JSONObject jsonObject : jsonObjects) {
                if (Objects.equals(jsonObject.getString("game_id"), gameId)) {
                    game_id = jsonObject.getString("game_id");
                    business_id = jsonObject.getString("business_id");
                    break;
                }
            }
        }
        if (StringUtils.isEmpty(game_id)|| StringUtils.isEmpty(business_id)){
            throw new GlobalException("为获取到游戏属性");
        }
        String url = "http://api.hhaohao.com/open/game/properties";
        Map<String, String> params = new HashMap<>();
        params.put("open_id", open_id);
        params.put("key", key);
        params.put("game_id", game_id);
        params.put("business_id", business_id);
        String result = HttpUtil.doGet(url, params, null);
        JSONObject resJson = JSONObject.parseObject(result);
        JSONObject data = resJson.getJSONObject("data");
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        JSONObject properties = data.getJSONObject("properties");
        for (String k : properties.keySet()) {
            if (StringUtils.equals(type, "3")) {
                if (properties.getString(k).contains("]") && properties.getString(k).contains("[")) {
                    InterAttrRes res = new InterAttrRes();
                    res.setUuid(UUIDGenerator.generateUuid6());
                    res.setId(k);
                    res.setName(k);
                    res.setRequired(false);
                    interAttrResList.add(res);
                }
            } else {
                if (!properties.getString(k).contains("]") && !properties.getString(k).contains("[")) {

                    InterAttrRes res = new InterAttrRes();
                    res.setUuid(UUIDGenerator.generateUuid6());
                    res.setId(k);
                    res.setName(k);
                    res.setRequired(false);
                    String str = properties.getString(k);
                    List<String> strings = Arrays.asList(str.split(","));
                    List<InterAttrRes> interAttrResList1 = new ArrayList<>();

                    for (String k1 : strings) {
                        InterAttrRes res1 = new InterAttrRes();
                        res1.setUuid(UUIDGenerator.generateUuid6());
                        res1.setId(k1);
                        res1.setName(k1);
                        res1.setRequired(false);
                        interAttrResList1.add(res1);
                    }
                    res.setInterServerRes(interAttrResList1);
                    interAttrResList.add(res);
                }
            }
        }
        return interAttrResList;
    }

}